package com.cipta.lomob.adapter;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.cipta.lomob.R;
 
public class NavExpandDrawerAdapter extends BaseExpandableListAdapter {
 
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;
 
    public NavExpandDrawerAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
 
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
 
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
 
    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
 
        final String childText = (String) getChild(groupPosition, childPosition);
 
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_nav_child_menu, null);
        }
 
        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListChildItem);
 
        txtListChild.setText(childText);
        return convertView;
    }
 
    @Override
    public int getChildrenCount(int groupPosition) {
    	int count = 0;
    	if (this._listDataChild.get(this._listDataHeader.get(groupPosition))==null) {
			count = 0;
		} else {
			count = this._listDataChild.get(this._listDataHeader.get(groupPosition))
	                .size();
		}
    	
        return count;
    }
 
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
 
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
 
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
 
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
    	String headerTitle = (String) getGroup(groupPosition);
    	if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    _context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.drawer_list_item, null);
        }
         
//        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
        TextView txtCount = (TextView) convertView.findViewById(R.id.counter);
        View viewIndicator = (View)convertView.findViewById(R.id.ln_sidemenu_color_indicator);
         
//        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());        
        txtTitle.setText(headerTitle);
        
        // displaying count
        // check whether it set visible or not
//        if(navDrawerItems.get(position).getCounterVisibility()){
//        	txtCount.setText(navDrawerItems.get(position).getCount());
//        }else{
//        	// hide the counter view
//        	txtCount.setVisibility(View.GONE);
//        }
        txtCount.setVisibility(View.GONE);
        
        switch (groupPosition) {
			case 0:
				viewIndicator.setBackgroundResource(R.color.sidebar_purple);
			break;
			
			case 1:
				viewIndicator.setBackgroundResource(R.color.sidebar_green);
			break;
			
			case 2:
				viewIndicator.setBackgroundResource(R.color.sidebar_blue);
			break;
			
			case 3:
				viewIndicator.setBackgroundResource(R.color.sidebar_yellow);
			break;
			
			case 4:
				viewIndicator.setBackgroundResource(R.color.sidebar_pink);
			break;
			
			case 5:
				viewIndicator.setBackgroundResource(R.color.sidebar_chocolate);
			break;

		default:
			break;
		}
        
        return convertView;
    }
 
    @Override
    public boolean hasStableIds() {
        return false;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
